<template>
  <div class="app-container">
    <!-- 搜索区域 - 美化样式 -->
    <div class="search-container">
      <el-form 
        :model="queryParams" 
        ref="queryForm" 
        size="small" 
        :inline="true" 
        v-show="showSearch"
        label-width="68px"
        class="search-form"
      >
        <div class="search-header">
          <h3 class="search-title">笔记搜索</h3>
          <el-button 
            type="text" 
            icon="el-icon-arrow-up" 
            size="small" 
            @click="headerShow=!headerShow"
            class="toggle-btn"
          ></el-button>
        </div>
        
        <template v-if="headerShow">
          <el-form-item label="标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入标题"
              clearable
              @keyup.enter.native="handleQuery"
              class="custom-input"
            />
          </el-form-item>
          <el-form-item label="分类" prop="classify">
            <el-select 
              v-model="queryParams.classify" 
              placeholder="请选择" 
              clearable
              class="custom-select"
            >
              <el-option
                v-for="item in classifyOption"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="公开" prop="externalDisplay">
            <el-select 
              v-model="queryParams.externalDisplay" 
              placeholder="请选择" 
              clearable
              class="custom-select"
            >
              <el-option key="open" label="公开" value="1"></el-option>
              <el-option key="noOpen" label="不公开" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <div class="search-buttons">
              <el-button 
                type="primary" 
                icon="el-icon-search" 
                size="mini" 
                @click="handleQuery"
                class="search-btn"
              >搜索</el-button>
              <el-button 
                icon="el-icon-refresh" 
                size="mini" 
                @click="resetQuery"
                class="reset-btn"
              >重置</el-button>
              <el-button
                v-if="!isBringInNote"
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['note:note:add']"
                class="add-btn"
              >新增</el-button>
              <el-button
                v-else
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="chooseNode"
                class="add-btn"
              >选择</el-button>
            </div>
          </el-form-item>
        </template>
      </el-form>
    </div>

    <!-- 表格区域 - 美化样式 -->
    <div class="table-container">
      <el-table 
        v-loading="loading" 
        :data="noteList" 
        @selection-change="handleSelectionChange"
        border
        class="custom-table"
        row-class-name="table-row"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column 
          width="600" 
          label="标题" 
          prop="title" 
          :show-overflow-tooltip="true"
          class-name="title-column"
        >
          <template slot-scope="scope">
            <div class="title-content">
              <span class="title-text">{{ scope.row.title }}</span>
              <el-badge 
                v-if="scope.row.externalDisplay === 1" 
                type="success" 
                class="external-badge"
                size="small"
                :value="'公开'"
              ></el-badge>
            </div>
          </template>
        </el-table-column>
        <el-table-column 
          width="85" 
          label="分类" 
          align="center" 
          prop="classifyStr"
        >
          <template slot-scope="scope">
            <el-tag 
              :type="getClassifyType(scope.row.classify)"
              size="small"
              class="classify-tag"
            >{{ scope.row.classifyStr }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column 
          label="标签" 
          width="120" 
          align="center"
        >
          <template slot-scope="scope">
            <el-popover 
              v-if="JSON.parse(scope.row.tag)!==null&&JSON.parse(scope.row.tag).length!==0"
              placement="top"
              title="标签"
              width="500"
              trigger="click"
              popper-class="tag-popover"
            >
              <el-button 
                slot="reference" 
                size="mini"
                type="text"
                class="tag-view-btn"
              >查看</el-button>
              <div class="tag-list">
                <el-tag
                  :key="tag.name"
                  v-for="tag in JSON.parse(scope.row.tag)"
                  :disable-transitions="false"
                  :type="tag.type"
                  :color="tag.color"
                  class="tag-item"
                >
                  {{ tag.name }}
                </el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column 
          label="合集" 
          align="center" 
          prop="remark" 
          :show-overflow-tooltip="true"
          class-name="compilation-column"
        >
          <template slot-scope="scope">
            <div class="compilation-list">
              <el-link 
                v-for="(item, index) in scope.row.compilations" 
                :key="index"
                type="success" 
                @click="openCompilation(item.id)"
                class="compilation-link"
                :underline="false"
              >《{{ item.title }}》</el-link>
            </div>
          </template>
        </el-table-column>
        <el-table-column 
          label="对外展示" 
          align="center" 
          prop="externalDisplay"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.externalDisplay"
              @change="changeExternalDisplay(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              class="display-switch"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column 
          label="操作" 
          align="center" 
          class-name="action-column"
        >
          <template slot-scope="scope">
            <div class="action-buttons">
              <el-button
                v-if="!isBringInNote"
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['note:note:edit']"
                class="action-btn edit-btn"
                circle
              ></el-button>
              <el-button
                v-if="!isBringInNote && scope.row.classify===108"
                size="mini"
                type="info"
                icon="el-icon-folder-opened"
                @click="downNote(scope.row)"
                v-hasPermi="['note:note:remove']"
                class="action-btn open-btn"
                circle
              ></el-button>
              <el-button
                v-else-if="!isBringInNote && scope.row.classify===107"
                size="mini"
                type="warning"
                icon="el-icon-download"
                @click="downNote(scope.row)"
                v-hasPermi="['note:note:remove']"
                class="action-btn download-btn"
                circle
              ></el-button>
              <el-button
                v-else-if="!isBringInNote"
                size="mini"
                type="success"
                icon="el-icon-view"
                @click="showNote(scope.row)"
                v-hasPermi="['note:note:remove']"
                class="action-btn view-btn"
                circle
              ></el-button>
              <el-button
                v-if="!isBringInNote"
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['note:note:remove']"
                class="action-btn delete-btn"
                circle
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页组件 - 美化样式 -->
    <div class="pagination-container">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        class="custom-pagination"
      />
    </div>

    <!-- 笔记详情弹窗 -->
    <NoteInfo 
      v-if="noteInfoOpen" 
      :item="item" 
      :open="noteInfoOpen" 
      @close="()=>{this.noteInfoOpen=false}"
    ></NoteInfo>

    <!-- 添加或修改笔记对话框 -->
    <el-dialog 
      v-if="open" 
      :title="title" 
      :visible.sync="open" 
      width="1000px" 
      append-to-body 
      :fullscreen="true"
      custom-class="note-dialog"
    >
      <addNote 
        @getList="getList" 
        @notOpen="notOpen" 
        @addNote="(e)=>{}" 
        :title="title" 
        :open="open" 
        :_note="note"
      />
    </el-dialog>
  </div>
</template>

<script>
import {delNote, listNote, updateNote} from '@/api/note/note'
import Dict from '@/views/system/dict'
import {getToken} from '@/utils/auth'
import music from './music'
import addNote from './addNote'
import {listData} from '@/api/system/dict/data'
import NoteInfo from '@/views/note/note/NoteInfo'
import {Base64} from 'js-base64'

export default {
  name: 'Index',
  props: ['isBringInNote'],
  components: {Dict, music, addNote, NoteInfo},
  data() {
    return {
      authorization: {Authorization: 'Bearer ' + getToken()}, // 上传的图片服务器地址
      uploadUrl: process.env.VUE_APP_BASE_API + '/file/upload', // 上传的图片服务器地址
      //浮动组件的配置
      floatForm: {
        themeColor: {
          hsl: {h: 200, s: 0, l: 0, a: 1},
          hex: '#000000',
          hex8: '#000000FF',
          rgba: {r: 0, g: 0, b: 0, a: 1},
          hsv: {h: 200, s: 0, v: 0, a: 1},
          oldHue: 200,
          source: 'hex',
          a: 1
        },
        fontSize: 20
      },
      headerShow: true,
      typeList: [],
      classifyOption: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 笔记表格数据
      noteList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      noteInfoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        tag: [],
        classify: null,
        tree: null,
        deleted: null,
        externalDisplay: null,
        newLabel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 标签
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      note: {},
      item: {},
      //标签的类型
      tagTypes: ['success', 'info', 'warning', 'danger']
    }
  },
  created() {
    listData({'dictType': 'zone_note_classify', 'pageSize': 99999}).then(response => {
      this.classifyOption = response.rows
    })
    this.handleQuery()
  },
  methods: {
    // 根据分类获取对应的标签类型
    getClassifyType(classify) {
      const typeMap = {
        101: 'primary',
        102: 'success',
        103: 'warning',
        104: 'danger',
        105: 'info',
        106: 'primary',
        107: 'warning',
        108: 'info'
      }
      return typeMap[classify] || 'info'
    },
    
    //处理引用合集的字符串
    handleCompilations(compilations) {
      if (compilations) {
        let strArr = []
        compilations.forEach(compilation => {
          strArr.push('《' + compilation.title + '》')
        })
        return strArr.join(',')
      }
      return null
    },
    
    changeExternalDisplay(row) {
      this.$modal.confirm('是否确认改变笔记编号为"' + row.id + '"的展示状态吗？').then(() => {
        return updateNote({
          id: row.id,
          externalDisplay: row.externalDisplay
        })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('改变成功')
      }).catch(() => {
        row.externalDisplay = (row.externalDisplay + 1) % 2
      })
    },
    
    //查看合集内容
    openCompilation(id) {
      this.$tab.openPage('title', '/note/compilation/CompilationStep?id=' + id)
    },
    
    notOpen() {
      this.open = false
    },
    
    showNote(row) {
      this.item = row
      this.noteInfoOpen = !this.noteInfoOpen
    },
    
    downNote(row) {
      //下载
      console.info(row.content)
      window.open(Base64.decode(row.content), '_self ')
    },
    
    closeNote() {
      this.noteInfoOpen = false
    },
    
    handleAdd() {
      this.note = null
      this.reset()
      this.open = true
      this.title = '添加笔记'
    },
    
    // 表单重置
    async reset() {
      this.fileList = []
      this.form = {
        id: null,
        title: null,
        content: null,
        tag: null,
        classify: null,
        treeId: null,
        deleted: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        newLabel: null,
        remark: null
      }
      this.resetForm('form')
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    
    //关闭标签
    handleClose(tag) {
      console.log('this.form.tag', this.form.tag)
      // let parse = JSON.parse(this.form.tag);
      this.form.tag.splice(this.form.tag.indexOf(tag), 1)
    },
    
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除笔记编号为"' + ids + '"的数据项？').then(() => {
        return delNote(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    
    /** 查询笔记列表 */
    getList() {
      this.loading = true
      console.log('getList', this.queryParams)
      listNote(this.queryParams).then(response => {
        this.noteList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.note = {
        note: row,
        noteId: row.id
      }

      this.title = '修改笔记'
      this.open = true
    },
    
    chooseNode() {
      this.$emit('chooseNoteIds', this.ids)
    }
  }
}
</script>

<style scoped>
/* 全局容器样式 */
.app-container {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
  transition: all 0.3s ease;
}

/* 搜索区域样式 */
.search-container {
  margin-bottom: 20px;
}

.search-form {
  background: #ffffff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.search-form:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.search-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.search-title {
  margin: 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.toggle-btn {
  color: #606266;
  transition: color 0.3s ease;
}

.toggle-btn:hover {
  color: #409eff;
}

/* 表单元素样式 */
.custom-input,
.custom-select {
  min-width: 200px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.custom-input:focus,
.custom-select:focus {
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

/* 搜索按钮组 */
.search-buttons {
  display: flex;
  gap: 8px;
}

.search-btn,
.reset-btn,
.add-btn {
  transition: all 0.3s ease;
  border-radius: 6px;
}

.search-btn:hover,
.add-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(64, 158, 255, 0.3);
}

.reset-btn:hover {
  transform: translateY(-1px);
}

/* 表格容器样式 */
.table-container {
  background: #ffffff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.table-container:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

/* 自定义表格样式 */
.custom-table {
  border-radius: 8px;
  overflow: hidden;
}

.table-row {
  transition: all 0.3s ease;
}

.table-row:hover {
  background-color: #fafafa;
  transform: scale(1.005);
}

/* 表格列样式 */
.title-column {
  font-weight: 600;
}

.title-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.external-badge {
  transform: scale(0.8);
}

/* 分类标签样式 */
.classify-tag {
  border-radius: 10px;
  font-size: 12px;
  padding: 0 10px;
}

/* 标签弹窗样式 */
.tag-popover {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.tag-view-btn {
  transition: color 0.3s ease;
}

.tag-view-btn:hover {
  color: #409eff;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px;
}

.tag-item {
  border-radius: 4px;
  margin: 0;
  transition: all 0.3s ease;
}

.tag-item:hover {
  transform: scale(1.05);
}

/* 合集链接样式 */
.compilation-column {
  font-size: 13px;
}

.compilation-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.compilation-link {
  transition: all 0.3s ease;
  border-radius: 4px;
  padding: 2px 6px;
}

.compilation-link:hover {
  background-color: #ecf5ff;
  transform: scale(1.05);
}

/* 对外展示开关 */
.display-switch {
  transform: scale(0.9);
}

/* 操作列样式 */
.action-column {
  width: 180px;
}

.action-buttons {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.action-btn {
  transition: all 0.3s ease;
  transform: scale(0.9);
}

.action-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.edit-btn:hover {
  background-color: #409eff;
}

.open-btn:hover {
  background-color: #909399;
}

.download-btn:hover {
  background-color: #e6a23c;
}

.view-btn:hover {
  background-color: #67c23a;
}

.delete-btn:hover {
  background-color: #f56c6c;
}

/* 分页样式 */
.pagination-container {
  display: flex;
  justify-content: flex-end;
}

.custom-pagination {
  margin-top: 10px;
}

/* 对话框样式 */
.note-dialog {
  border-radius: 12px;
  overflow: hidden;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .app-container {
    padding: 15px;
  }
  
  .custom-input,
  .custom-select {
    min-width: 150px;
  }
}

@media (max-width: 768px) {
  .app-container {
    padding: 10px;
  }
  
  .search-form,
  .table-container {
    padding: 15px;
  }
  
  .search-buttons {
    flex-wrap: wrap;
  }
  
  .custom-input,
  .custom-select {
    min-width: 100%;
  }
  
  .action-column {
    width: 160px;
  }
  
  .action-btn {
    transform: scale(0.8);
  }
}

/* 滚动条美化 */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
  transition: background 0.3s ease;
}

::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>
